package com.jfee.dds.vo;

import com.alibaba.fastjson.JSON;
import com.jfee.dds.enums.ResultStatusCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * 响应工具类。
 *
 * @author sf.xiong
 */
@Setter
@Getter
public class Result<T> {

    private int code = ResultStatusCode.OK.getCode();
    private String msg = ResultStatusCode.OK.getMsg();
    private T data;

    public Result() {

    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(ResultStatusCode resultStatusCode, T data) {
        this(resultStatusCode.getCode(), resultStatusCode.getMsg(), data);
    }

    public Result(int code, String msg) {
        this(code, msg, null);
    }

    public Result(ResultStatusCode resultStatusCode) {
        this(resultStatusCode, null);
    }

    public void setErr() {
        this.code = ResultStatusCode.FAIL.getCode();
        this.msg = ResultStatusCode.FAIL.getMsg();
    }

    public void setCodeAndMsg(ResultStatusCode resultStatusCode) {
        this.code = resultStatusCode.getCode();
        this.msg = resultStatusCode.getMsg();
    }

    public void setCodeAndMsg(int code, String msg) {
        if (code != -1) {
            this.code = ResultStatusCode.OK.getCode();
        } else {
            this.code = ResultStatusCode.FAIL.getCode();
        }
        if (StringUtils.isNotBlank(msg)) {
            this.msg = msg;
        } else {
            this.msg = ResultStatusCode.FAIL.getMsg();
        }
    }

    public String toJSONString() {
        return JSON.toJSONString(this);
    }

}
